package algorithm;

public class leetcode_0134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int len= gas.length;
        int sum=0;
        for(int i=0;i<len;++i){
            sum+=gas[i]-cost[i];
        }
        if(sum<0) return -1;
        for(int i=0;i<len;++i){
            sum=0;
            int count=0;
            while(count<len){
                sum+=gas[(i+count)%len]-cost[(i+count)%len];
                count++;
                if(sum<0) {
                    i=i+count-1;
                    break;
                }
            }
            if(count==len) return i;
        }
        return 0;
    }
}
